/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.ad.restws;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import ua.ad.feedsreader.mybatis.config.MyBatisSqlSessionFactory;
import ua.ad.feedsreader.mybatis.mappers.beans.Categoria;
import ua.ad.feedsreader.mybatis.mappers.beans.ItemFeed;
import ua.ad.feedsreader.mybatis.mappers.beans.UserItemFeed;
import ua.ad.feedsreader.mybatis.mappers.clients.CategoriaMapper;
import ua.ad.feedsreader.mybatis.mappers.clients.ItemFeedMapper;
import ua.ad.feedsreader.mybatis.mappers.clients.UserItemFeedMapper;

/**
 *
 * @author sexta13
 */
public class StatsService {

    public StatsService() {
    }

    public Stat devolveItemFeeds(long dateS, long dateE) {
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        Date dateStart = new Date(dateS);
        Date dateEnd = new Date(dateE);

        SqlSession bdSession = MyBatisSqlSessionFactory.getSqlSessionFactory().openSession();

        ItemFeedMapper itMapper = bdSession.getMapper(ItemFeedMapper.class);
        List<ItemFeed> lstItemFeed = null;
         if (dateS == 0 && dateE == 0) {
            lstItemFeed = itMapper.selectAllItemFeeds();
        } else if (dateS != 0 && dateE == 0) {                                    
           lstItemFeed = itMapper.selectAllItemFeedsInRange(df.format(dateStart), df.format(new Date()));
        } else if (dateS != 0 && dateE != 0) {
            lstItemFeed = itMapper.selectAllItemFeedsInRange(df.format(dateStart), df.format(dateEnd));
        }


        Stat stat = new Stat();
        stat.setAvg(1);
        stat.setCount(lstItemFeed.size());
        stat.setDesc("Devolve o numero de items carregados no sistema");
        stat.setName("nr item feeds");
        bdSession.close();
        return stat;
    }

    public Stat devolveItemFeedsPartilhados() {

        SqlSession bdSession = MyBatisSqlSessionFactory.getSqlSessionFactory().openSession();

        ItemFeedMapper itMapper = bdSession.getMapper(ItemFeedMapper.class);
        List<ItemFeed> lstItemFeed = itMapper.selectAllItemFeedsShared();

        Stat stat = new Stat();
        stat.setAvg(1);
        stat.setCount(lstItemFeed.size());
        stat.setDesc("Devolve o numero de items partilhados pelos utilizadores");
        stat.setName("nr item feeds partilhados");
        bdSession.close();
        return stat;
    }

    public Stat devolveItemFeedsLidos() {

        SqlSession bdSession = MyBatisSqlSessionFactory.getSqlSessionFactory().openSession();

        UserItemFeedMapper uifMapper = bdSession.getMapper(UserItemFeedMapper.class);
        List<UserItemFeed> lstUserItemFeed = uifMapper.selectAllItemFeedRead();

        
        Stat stat = new Stat();
        stat.setAvg(1);
        stat.setCount(lstUserItemFeed.size());
        stat.setDesc("Devolve o numero de items lidos pelos utilizadores");
        stat.setName("nr item feeds lidos");
        bdSession.close();
        return stat;
    }

    public Stat devolveCategorias(long dateS, long dateE) {
        
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        Date dateStart = new Date(dateS);
        Date dateEnd = new Date(dateE);

        SqlSession bdSession = MyBatisSqlSessionFactory.getSqlSessionFactory().openSession();

        CategoriaMapper cMapper = bdSession.getMapper(CategoriaMapper.class);
        List<Categoria> lstCategorias = null;
        
        if (dateS == 0 && dateE == 0) {
            lstCategorias = cMapper.selectAllCategories();
        } else if (dateS != 0 && dateE == 0) {                                    
           lstCategorias = cMapper.selectAllCategoriesInRange(df.format(dateStart), df.format(new Date()));
        } else if (dateS != 0 && dateE != 0) {
            lstCategorias = cMapper.selectAllCategoriesInRange(df.format(dateStart), df.format(dateEnd));
        }

        Stat stat = new Stat();
        stat.setAvg(1);
        stat.setCount(lstCategorias.size());
        stat.setDesc("Devolve o numero de categorias");
        stat.setName("nr categorias");
        bdSession.close();
        return stat;
    }
}
